package com.mt.sdk.tools;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import android.os.Environment;

public class MTTools {

	// byte转换成字符串
	public static String convertByteToString(byte byteToConvert) {
		String ConvertedByte = "";
		if (byteToConvert < 0) {
			ConvertedByte += Integer.toString(byteToConvert + 256, 16);
		} else if (byteToConvert <= 15) {
			ConvertedByte += "0" + Integer.toString(byteToConvert, 16);
		} else {
			ConvertedByte += Integer.toString(byteToConvert, 16);
		}

		return ConvertedByte.toUpperCase();
	}

	// byte[]转换成字符串
	public static String convertBytearrayToString(byte[] data) {

		if (data == null) {
			return null;
		}

		String str = "";
		for (int i = 0; i < data.length; i++) {
			str += convertByteToString(data[i]) + " ";
		}

		return str;
	}

	// string转换成byte
	public static byte convertCharToByte(char hvalue, char lvalue) {
		if (!(((hvalue >= '0') && (hvalue <= '9')) || ((hvalue >= 'a') && (hvalue <= 'f'))
				|| ((hvalue >= 'A') && (hvalue <= 'F')))) {
			return 0;
		}
		if (!(((lvalue >= '0') && (lvalue <= '9')) || ((lvalue >= 'a') && (lvalue <= 'f'))
				|| ((lvalue >= 'A') && (lvalue <= 'F')))) {
			return 0;
		}

		byte value = 0;

		if ((hvalue <= '9') && (hvalue >= '0')) {
			value = (byte) (((hvalue - '0') * 16) & 0xFF);
		} else {
			value = (byte) (((hvalue - 'A' + 10) * 16) & 0xFF);
		}

		if ((lvalue <= '9') && (lvalue >= '0')) {
			value |= (byte) ((lvalue - '0') & 0xFF);
		} else {
			value |= (byte) ((lvalue - 'A' + 10) & 0xFF);
		}

		return value;
	}

	// string转换成byte[]
	public static byte[] convertStringToByteArray(String str) {
		if(str == null){
			return null;
		}
		
		String str_tmp = str.toUpperCase();
		char hvalue;
		char lvalue;
		ByteArrayOutputStream out = new ByteArrayOutputStream();

		for (int i = 0; i < str_tmp.length();) {
			hvalue = str.charAt(i); // 高位

			if (((hvalue <= '9') && (hvalue >= '0')) || ((hvalue <= 'F') && (hvalue >= 'A'))) {
				if ((i + 1) >= str_tmp.length()) {
					lvalue = '0';
				} else {
					lvalue = str.charAt(i + 1);
					if (((lvalue <= '9') && (lvalue >= '0')) || ((lvalue <= 'F') && (lvalue >= 'A'))) {

					} else {
						lvalue = '0';
					}
				}
				i += 2;
				out.write(convertCharToByte(hvalue, lvalue));
			} else {
				i++;
			}
		}

		return out.toByteArray();
	}

	// uuid由byte[]转换成String形式
	public static String converUUIDByteToString(byte[] uuid_byte) {

		if ((uuid_byte == null) || (uuid_byte.length != 16)) {
			return null;
		}

		String uuid_str = convertByteToString(uuid_byte[0]) + convertByteToString(uuid_byte[1])
				+ convertByteToString(uuid_byte[2]) + convertByteToString(uuid_byte[3]) + "-"
				+ convertByteToString(uuid_byte[4]) + convertByteToString(uuid_byte[5]) + "-"
				+ convertByteToString(uuid_byte[6]) + convertByteToString(uuid_byte[7]) + "-"
				+ convertByteToString(uuid_byte[8]) + convertByteToString(uuid_byte[9]) + "-"
				+ convertByteToString(uuid_byte[10]) + convertByteToString(uuid_byte[11])
				+ convertByteToString(uuid_byte[12]) + convertByteToString(uuid_byte[13])
				+ convertByteToString(uuid_byte[14]) + convertByteToString(uuid_byte[15]);

		return uuid_str;
	}

	// 长数据截取成短数据
	public static List<byte[]> cutLongDatasToShort(byte[] datas, int tolenght) {

		List<byte[]> sendcmd = new ArrayList<byte[]>();

		for (int i = 0; i < datas.length / tolenght; i++) {
			byte[] cmd = new byte[tolenght];
			for (int j = 0; j < tolenght; j++) {
				cmd[j] = datas[i * tolenght + j];
			}
			sendcmd.add(cmd);
		}
		if (datas.length % tolenght != 0) {
			int start = datas.length / tolenght * tolenght;
			byte[] cmd = new byte[datas.length % tolenght];
			for (int j = 0; j < datas.length % tolenght; j++) {
				cmd[j] = datas[start + j];
			}
			sendcmd.add(cmd);
		}

		return sendcmd;
	}

	public static int convertUint32(byte b0, byte b1, byte b2, byte b3) {
		int value = 0;
		value |= (b3 & 0xFF);
		value <<= 8;
		value |= (b2 & 0xFF);
		value <<= 8;
		value |= (b1 & 0xFF);
		value <<= 8;
		value |= (b0 & 0xFF);
		return value;
	}

	public static int convertUint16(byte hb, byte lb) {
		int value = 0;
		value |= (hb & 0xff);
		value <<= 8;
		value |= (lb & 0xff);
		return value;
	}

	public static int convertUint8(byte b) {
		int value = 0;
		value |= (b & 0xff);
		return value;
	}

	public static byte convertIntToByte(int i) {
		return (byte) (i & 0xFF);
	}

	public static byte converLoUint16(int i){
		return (byte) ((i & 0xff) >> 0);
	}
	public static byte converHiUint16(int i){
		return (byte) ((i & 0xff00) >> 8);
	}
	
	// 拷贝数组
	public static void arraycopy(Object src, int srcPos, Object dst, int dstPos, int length) {
		System.arraycopy(src, srcPos, dst, dstPos, length);
	}

	// 打印信息
	public static void logout(String log) {
		System.out.println(log);

		// writeStr(log);
	}

	// 判断2个数组是否相等
	public static boolean bytesEquals(byte[] bytes1, byte[] bytes2){
		if((bytes1 == null) || (bytes2 == null)){
			return false;
		}
		
		return Arrays.equals(bytes1, bytes2);
	}
	
	// 写数据到文件
	private final static String file_path = Environment.getExternalStorageDirectory().getAbsolutePath();

	private static void writeStr(String str) {
		File file = new File(file_path, "MTBeacon.txt");
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日    HH:mm:ss  ");
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		String now_time = formatter.format(curDate);

		try {
			FileOutputStream fOut = new FileOutputStream(file, true);
			OutputStreamWriter osw = new OutputStreamWriter(fOut);
			osw.write(now_time + "    " + str + "\r\n");
			osw.flush();
			osw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
